package com.hqyj.springboot.service.impl;

import com.hqyj.springboot.service.TaskService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.concurrent.Future;

@Service
public class TaskServiceImpl implements TaskService {

    @Override
    public Future<Integer> doSynchronizedTask() throws Exception {
        //启动一个线程；
        System.out.println("doSynchronizedTask thread: "+Thread.currentThread().getName());
        // 异步线程延时 5s 返回结果
        Thread.sleep(5000);
        Random random = new Random();
        //生成一个60-100之间的随机数；
        //0-60
        return new AsyncResult<>(random.nextInt(41)+60);
        //return null;
    }

    @Async
    @Override
    public Future<Integer> doAsynchronizedTask() throws Exception {
        System.out.println("doAsynchronizedTask thread: "+Thread.currentThread().getName());
        // 异步线程延时 5s 返回结果
        Thread.sleep(5000);
        Random random = new Random();
        //生成一个60-100之间的随机数；
        //0-60
        return new AsyncResult<>(random.nextInt(41)+60);
    }
}
